<?php
/*
 * This file is part of SoftSite.
 *
 * (c) 2012 Steve Tredinnick
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * @Desc Button Element
 *
 * @package softsite
 * @author  Steve Tredinnick <steve.tredinnick@gmail.com>
 */

namespace Lib\Forms\Elements;

use Lib\Forms\BaseElement;

if (!defined('IN_APP'))
{
    Die("Direct Access to PHP Scripts is not permitted");
}

/**
 * @Desc HTML Button for use within the template engine.
 */
class ButtonElement extends BaseElement
{

    /**
     * @var string Name of the button
     */
    private $name = "";

    /**
     * @var string Button face Text
     */
    private $buttonText = "";

    /**
     * @var string value of the button
     */
    private $value = "";

    /**
     * @var string type of the button
     */
    private $type = "";

    /**
     * @var string autofocus state of the button
     */
    private $autoFocus = "";

    /**
     * @var string disabled state of the button
     */
    private $disabled = "";

    // TODO: Implement Button $form - Would add functionality to add button across multiple forms.
    //private $form="";

    /**
     * @var string Form action of the button
     */
    private $formAction = "";

    // TODO: Implement Button $formEncType - Little Browser support ATM.
    //private $formEncType="";

    /**
     * @var string form method of the button
     */
    private $formMethod = "";

    /**
     * @var string form no validate attribute of the button
     */
    private $formNoValidate = "";

    /**
     * @var string form target of the button
     */
    private $formTarget = "";

    /**
     * @var string button image for the face of the form.
     */
    //private $buttonImage="";

    /**
     * @desc Generates the HTML required for the Button
     */
    private function GenerateHtml()
    {
        $this->ValidateAttributes();

        $this->formHtml .= "<button";
        $this->formHtml .= $this->name;
        $this->formHtml .= $this->type;
        $this->formHtml .= $this->value;
        $this->formHtml .= $this->autoFocus;
        $this->formHtml .= $this->disabled;
        $this->formHtml .= $this->formMethod;
        $this->formHtml .= $this->formNoValidate;
        $this->formHtml .= $this->formAction;
        $this->formHtml .= $this->formTarget;
        $this->formHtml .= ">";
        $this->formHtml .= $this->buttonText;
        $this->formHtml .= "</button>";

    }

    /**
     * Validates that the button contains valid atrribute combinations before the button is submitted
     */
    private function ValidateAttributes()
    {
        //Check if a form action is only being used with a button type of submit.
        if (!$this->formAction == "")
        {
            if (!$this->type == "Submit")
            {
                $this->error->ThrowError(WARNING, "Button Form Action can only be used with a button type of Submit", $_SERVER['PHP_SELF'], __LINE__);
            }
        }

        //Check if a form method is only being used with a button type of submit.
        if (!$this->formMethod == "")
        {
            if (!$this->type == "Submit")
            {
                $this->error->ThrowError(WARNING, "Button Form Method can only be used with a button type of Submit", $_SERVER['PHP_SELF'], __LINE__);
            }
        }

        //Check if form no validate is only being used with a button type of submit.
        if (!$this->formNoValidate == "")
        {
            if (!$this->type == "Submit")
            {
                $this->error->ThrowError(WARNING, "Button formnovalidate can only be used with a button type of Submit", $_SERVER['PHP_SELF'], __LINE__);
            }
        }

        //Check if a form target is only being used with a button type of submit.
        if (!$this->formTarget == "")
        {
            if (!$this->type == "Submit")
            {
                $this->error->ThrowError(WARNING, "Button Target can only be used with a button type of Submit", $_SERVER['PHP_SELF'], __LINE__);
            }
        }
    }

    /**
     * @desc Returns the generated HTML for the view.
     * @return string Generated HTML for the view.
     */
    public function RenderElement()
    {
        $this->GenerateHtml();
        return $this->formHtml;
    }

    /**
     * Sets the autofocus attribute on the button
     *
     * The autofocus attribute is a boolean attribute.
     *
     * When present, it specifies that a button should automatically get focus when the page loads.
     */
    public function SetAutoFocus()
    {
        $this->autoFocus = " autofocus ";
    }

    /**
     * Sets the disabled attribute on the button
     *
     * The disabled attribute is a boolean attribute.
     *
     * When present, it specifies that the button should be disabled.
     *
     * A disabled button is unusable and un-clickable.
     *
     * The disabled attribute can be set to keep a user from clicking on the button until some other condition has been met
     * (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the button usable.
     */
    public function SetDisabled()
    {
        $this->disabled = " disabled ";
    }

    /**
     * Sets the formaction attribute on the button.
     *
     * The formaction attribute specifies where to send the form-data when a form is submitted. This attribute overrides the form's action attribute.
     *
     * The formaction attribute is only used for buttons with type="submit".
     *
     * MONITOR: NOT CURRENTLY SUPPORTED IN INTERNET EXPLORER.
     *
     * @param $url string Url of the form action.
     */
    public function SetFormAction($url)
    {
        $this->formAction = "formaction='" . $url . "'";
    }

    /**
     * Sets the formmethod attribute of the button
     *
     * The formmethod attribute specifies which HTTP method to use when sending the form-data. This attribute overrides the form's method attribute.
     *
     * The formmethod attribute is only used for buttons with type="submit".
     *
     * MONITOR: NOT CURRENTLY SUPPORTED IN INTERNET EXPLORER.
     * @param $method string POST/GET method
     * @value
     */
    public function SetFormMethod($method)
    {
        //convert method to uppercase
        $method = strtoupper($method);

        //check if the method contains valid attributes otherwise throw warning to the user.
        if ($method == "POST" || $method == "GET")
        {
            $this->formMethod = " formmethod='" . $method . "'";
        }
        else
        {
            $this->error->ThrowError(WARNING, "Invalid Method type of " . $method . " expecting POST|GET as a valid value", $_SERVER['PHP_SELF'], __LINE__);
        }
    }

    /**
     * Sets the buttons formnovalidate attribute.
     *
     * The formnovalidate attribute is a boolean attribute.
     *
     * When present, it specifies that the form-data should not be validated on submission. This attribute overrides the form's novalidate attribute
     *
     * The formnovalidate attribute is only used for buttons with type="submit".
     *
     * MONITOR: NOT CURRENTLY SUPPORTED IN INTERNET EXPLORER OR SAFARI.
     *
     */
    public function SetFormNoValidate()
    {
        $this->formNoValidate = " formnovalidate ";
    }


    /**
     * Sets the formtarget attribute of the button to _blank, _self, _parent, _top
     *
     * If you want to set the formtarget to a frame use SetFormTargetFrame instead.
     *
     * The formtarget attribute specifies where to display the response after submitting the form. This attribute overrides the form's target attribute.
     * The formtarget attribute is only used for buttons with type="submit".
     *
     * MONITOR: NOT CURRENTLY SUPPORTED IN INTERNET EXPLORER.
     *
     * @param $target string Target of the button valid values are _blank, _self, _parent, _top
     */
    public function SetFormTarget($target)
    {
        //check if the attribute contains valid values otherwise throw warning to the user.
        if ($target == "_blank" || $target == "_self" || $target == "_parent" || $target == "_top")
        {
            $this->formTarget = " formtarget='" . $target . "'";
        }
        else
        {
            $this->error->ThrowError(WARNING, "Invalid form target of " . $target . " expecting blank|_self|_parent|_top as a valid value", $_SERVER['PHP_SELF'], __LINE__);
        }
    }

    /**
     * Sets the formtarget attribute of the button to a frame
     *
     * If you want to set the formtarget to _blank, _self, _parent, _top use SetFormTarget Instead.
     *
     * The formtarget attribute specifies where to display the response after submitting the form. This attribute overrides the form's target attribute.
     * The formtarget attribute is only used for buttons with type="submit".
     *
     * MONITOR: NOT CURRENTLY SUPPORTED IN INTERNET EXPLORER.
     *
     * @param $targetFrame string Target Frame of the button
     */
    public function SetFormTargetFrame($targetFrame)
    {
        $this->formTarget = " formtarget='" . $targetFrame . "' ";
    }

    /**
     * The name attribute specifies the name for a <button> element.
     *
     * The name attribute is used to reference form-data after the form has been submitted, or to reference the element in a JavaScript.
     *
     * Tip: Several <button> elements can share the same name. This allows you to have several buttons with equal names, which can submit different values when used in a form.
     *
     * @param $name string Name of the button.
     */
    public function SetName($name)
    {
        $this->name = " name='" . $name . "' ";
    }

    /**
     * Sets the type attribute for the button
     *
     * The type attribute specifies the type of button.
     *
     * Tip: Always specify the type attribute for the button. Different browsers use different default values for the type attribute.
     *
     * @param $type string The type of the button valid values are button, submit or reset
     */
    public function SetType($type)
    {
        //check if the button type attribute contains valid values otherwise throw warning to the user.
        if ($type == "button" || $type == "submit" || $type == "reset")
        {
            $this->type = " type='" . $type . "' ";
        }
        else
        {
            $this->error->ThrowError(WARNING, "Invalid Button Type of " . $type . " expecting button|submt|reset as a valid value", $_SERVER['PHP_SELF'], __LINE__);
        }
    }

    /**
     * The value attribute specifies the initial value for a <button> in a <form>.
     * Note: In a form, the button and its value is only submitted if the button itself was used to submit the form.
     *
     * @param $value string Value of the button once submitted.
     */
    public function SetValue($value)
    {
        $this->value = "value='" . $value . "' ";
    }

    /**
     * Set the Text that should appear on the button face
     *
     * @param $buttonText
     */
    public function SetText($buttonText)
    {
        $this->buttonText = $buttonText;
    }

    /**
     * Sets the image that should appear on the button face
     *
     * @param $imageUrl string Url that contains the image file.
     */
    public function SetImage($imageUrl)
    {
        //TODO: Add Images to Buttons
    }


}
